//
//  YUNTagTableViewController.m
//  BaiSi
//
//  Created by warm on 17/9/5.
//  Copyright © 2017年 hp. All rights reserved.
//

#import "YUNTagTableViewController.h"
#import<AFNetworking/AFNetworking.h>
#import "YUNSubTagItem.h"

#import <MJExtension/MJExtension.h>

#import "YUNSubTagCell.h"
#import <SVProgressHUD.h>

//定义全局的ID，在这里为了不被修改所以加const

static NSString *const ID=@"cell";

@interface YUNTagTableViewController ()




//一个存放从模型中解析出来的数据的可变数组
@property(nonatomic,strong)NSMutableArray *subTags;


@property(nonatomic,weak)AFHTTPSessionManager *mgr;



@end

@implementation YUNTagTableViewController

//查看接口文档：主要是看,请求url(基本url+请求参数)、请求的方式



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //展示标签数据-》请求数据（接口文档）-->解析数据（image_list、sub_number、theme_name）->设计模型->字典转模型-》展示数据
    
    [self loadData];
    
    //另一种注册cell 的方式,这种方式不需要绑定标识ID，
    [self.tableView registerNib:[UINib nibWithNibName:@"YUNSubTagCell" bundle:nil ] forCellReuseIdentifier:ID];
    
    
       self.title=@"推荐标签";
    
    //处理Cell的分割线，1、自定义分割线，2、系统的属性来解决这个问题
    
    //提示用户当前正在加载数据SVPro
    [SVProgressHUD showWithStatus:@"正在加载中......"];
    

    
    
    
    
    
   }

//当界面即将消失的时候调用
-(void)viewWillDisappear:(BOOL)animated
{


    [super viewWillDisappear:animated];
    
    //销毁指示器
    [SVProgressHUD dismiss];
    
    
    //取消之前的请求
    [_mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
    


}



-(void)loadData
{

//1、创建请求会发管理者
    
     AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    
    _mgr=mgr;
    
    //2、拼接参数
    
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    
    parameters[@"a"]=@"tag_recommend";
    parameters[@"action"]=@"sub";
    parameters[@"c"]=@"topic";
   //
    //3、发送请求
    [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        
        //请求成功的时候要将用户提示器去除
        [SVProgressHUD dismiss];
        
        [responseObject writeToFile:@"/Users/warm/Desktop/baisi_github/BaiSiBuDeJie/BaiSi/BaiSi/Classes/New/tag.plist"  atomically:YES ];
        
       
        
        NSLog(@"%@",responseObject);
        
        
        //将解析出来的数据保存在一个可变数组中
       _subTags= [YUNSubTagItem  mj_objectArrayWithKeyValuesArray:responseObject];
        
        //刷新表格
        [self.tableView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        
        
        //请求失败的时候也要将用户指示器去除
        [SVProgressHUD dismiss];
    }];




}

#pragma mark -请求数据

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
    
    
    
    
    
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.subTags.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    //如果从这里xib中加载cell那么就一定要绑定标识id
    
    
      // static NSString *ID=@"cell";
    
    //自定义Cell
    
 YUNSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    
    //注册cell ,也可以用另一种进行注册
    /*
if(cell==nil)
{

      cell=[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YUNSubTagCell class]) owner:nil options:nil][0];
    //
   // 或者cell=[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YUNSubTagCell class]) owner:nil options:nil]lastObject;
    
    

}
    */
    //获取数据
    YUNSubTagItem *item=self.subTags[indexPath.row];
    
    cell.item=item;
    
 
    /*
     
     
     设置图片的圆角：裁剪图片
     设置数字：当数字达到一定程度的时候就将其缩率
     
     
     */
    
    //cell.textLabel.text=item.theme_name;
    
    
    return cell;
}

//自定义cell的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    return 80;


}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
